/**
 * @author Roger Wu v1.0
 * @author ZhangHuihua@msn.com 2011-4-1
 */
(function($) {
	function getObj(o, that) {
		var obj = {};
		for (var i = 0; i < o.length; i++) {
			var key = o[i];
			var value = $(that).attr(key);
			if (value) {
				obj[key] = value;
			} else {
				obj[key] = false;
			}
		}
		return obj;
	}
	$.fn.jTable = function(options) {
		var sets = new Set();
		this.getKeys = function() {
			return sets;
		}
		return this.each(function() {
			var $table = $(this),
				nowrapTD = $table.attr("nowrapTD");
			var rownum = $table.attr("rownum");
			if (rownum === "true") {
				$table.find("thead tr").prepend("<th width='20'></th>");
				$table.find("tbody tr").each(function(i) {
					$(this).prepend('<td align="center">' + (i + 1) + '</td>');
				});
			}
			var tlength = $table.width();
			var aStyles = [];
			var $tc = $table.parent().addClass("j-resizeGrid"); // table parent
			// container
			var layoutH = $(this).attr("layoutH");

			var oldThs = $table.find("thead>tr:last-child").find("th");
			for (var i = 0, l = oldThs.size(); i < l; i++) {
				var $th = $(oldThs[i]);
				var sWidth = $th.attr("width");
				var style = [],
					width =  sWidth ||   $th.innerWidth() - (100 * $th.innerWidth() / tlength) - 2;
				style[0] = parseInt(width);
				style[1] = $th.attr("align");
				aStyles[aStyles.length] = style;
			}
			$(this).wrap("<div class='grid'></div>");
			var $grid = $table.parent().html($table.html());
			var thead = $grid.find("thead");
			thead.wrap("<div class='gridHeader'><div class='gridThead'><table style='width:" + (tlength - 20) + "px;'></table></div></div>");
			var lastH = $(">tr:last-child", thead);
			var ths = $(">th", lastH);
			$("th", thead).each(function() {
				var $th = $(this);
				$th.html("<div class='gridCol' title='" + $th.text() + "'>" + $th.html() + "</div>");
			});
			ths.each(function(i) {
				var $th = $(this),
					style = aStyles[i];
				$th.addClass(style[1]).hoverClass("hover").removeAttr("align").removeAttr("width").width(style[0]);
			}).filter("[orderField]").orderBy({
				targetType: $table.attr("targetType"),
				rel: $table.attr("rel"),
				asc: $table.attr("asc") || "asc",
				desc: $table.attr("desc") || "desc"
			});

			var tbody = $grid.find(">tbody");
			var layoutStr = layoutH ? " layoutH='" + layoutH + "'" : "";

			tbody.wrap("<div class='gridScroller'" + layoutStr + " style='width:" + $tc.width() + "px;height:" + layoutH + "px'><div class='gridTbody'><table style='width:" + (tlength - 20) + "px;'></table></div></div>");
			var ftr = $(">tr:first-child", tbody);
			var $trs = tbody.find('>tr');

			$trs.hoverClass().each(function() {
				var $tr = $(this);
				var $ftds = $(">td", this);

				for (var i = 0; i < $ftds.size(); i++) {
					var $ftd = $($ftds[i]);
					if (nowrapTD != "false") {
						var html = $ftd.html();
						var formatData = $ftd.attr("formatData");
						if (formatData) {
							$ftd.html('<div formatData="' + formatData + '">' + html + '</div>');
						} else {
							$ftd.html("<div>" + html + "</div>");
						}
					}

					if (i < aStyles.length) $ftd.addClass(aStyles[i][1]);
				}
				$tr.click(function() {
					var key = $(this).attr("indexKey");
					//判断是否是单例
					if (options.single === true) {
						$trs.filter(".selected").removeClass("selected");
					}
					//判断是点击时间交替变化
					if (options.toggle === true) {
						if ($(this).hasClass("selected")) {
							$(this).removeClass("selected");
						} else {
							$tr.addClass("selected");
						}
					} else {
						$tr.addClass("selected");
					}

					if (options.save) {
						if ($(this).hasClass("selected")) {
							if ($.isArray(options.save)) {
								var obj = getObj(options.save, this);
								sets.add(obj);
							} else {
								sets.add(key);
							}

						} else {
							if ($.isArray(options.save)) {
								var obj = getObj(options.save);
								sets.remove(obj);
							} else {
								sets.remove(key);
							}
						}
					}
					var sTarget = $tr.attr("target");
					if (sTarget) {
						if ($("#" + sTarget, $grid).size() == 0) {
							$grid.prepend('<input id="' + sTarget + '" type="hidden" />');
						}
						$("#" + sTarget, $grid).val($tr.attr("rel"));
					}
				});
			});

			$(">td", ftr).each(function(i) {
				if (i < aStyles.length)
					$(this).width(aStyles[i][0]);
			});
			$grid.append("<div class='resizeMarker' style='height:300px; left:57px;display:none;'></div><div class='resizeProxy' style='height:300px; left:377px;display:none;'></div>");
			var scroller = $(".gridScroller", $grid);
			scroller.scroll(function(event) {
				var header = $(".gridThead", $grid);
				if (scroller.scrollLeft() > 0) {
					header.css("position", "relative");
					var scroll = scroller.scrollLeft();
					header.css("left", scroller.cssv("left") - scroll);
				}
				if (scroller.scrollLeft() == 0) {
					header.css("position", "relative");
					header.css("left", "0px");
				}
				return false;
			});

			$(">tr", thead).each(function() {

				$(">th", this).each(function(i) {
					var th = this,
						$th = $(this);
					$th.mouseover(function(event) {
						var offset = $.jTableTool.getOffset(th, event).offsetX;
						if ($th.outerWidth() - offset < 5) {
							$th.css("cursor", "col-resize").mousedown(
								function(event) {
									$(".resizeProxy", $grid).show().css({
										left: $.jTableTool.getRight(th) - $(".gridScroller", $grid)
											.scrollLeft(),
										top: $.jTableTool.getTop(th),
										height: $.jTableTool.getHeight(th,
											$grid),
										cursor: "col-resize"
									});
									$(".resizeMarker", $grid).show().css({
										left: $.jTableTool.getLeft(th) + 1 - $(".gridScroller", $grid)
											.scrollLeft(),
										top: $.jTableTool.getTop(th),
										height: $.jTableTool.getHeight(th,
											$grid)
									});
									$(".resizeProxy", $grid).jDrag($
										.extend(options, {
											scop: true,
											cellMinW: 20,
											relObj: $(".resizeMarker",
												$grid)[0],
											move: "horizontal",
											event: event,
											stop: function() {
												var pleft = $(
													".resizeProxy",
													$grid)
													.position().left;
												var mleft = $(
													".resizeMarker",
													$grid)
													.position().left;
												var move = pleft - mleft - $th
													.outerWidth() - 9;

												var cols = $.jTableTool
													.getColspan($th);
												var cellNum = $.jTableTool
													.getCellNum($th);
												var oldW = $th.width(),
													newW = $th
														.width() + move;
												var $dcell = $(">td",
													ftr).eq(cellNum - 1);

												$th.width(newW + "px");
												$dcell.width(newW + "px");

												var $table1 = $(thead)
													.parent();
												$table1.width(($table1
													.width() - oldW + newW) + "px");
												var $table2 = $(tbody)
													.parent();
												$table2.width(($table2
													.width() - oldW + newW) + "px");

												$(
													".resizeMarker,.resizeProxy",
													$grid).hide();
											}
										}));
								});
						} else {
							$th.css("cursor", $th.attr("orderField") ? "pointer" : "default");
							$th.unbind("mousedown");
						}
						return false;
					});
				});
			});

			function _resizeGrid() {
				$("div.j-resizeGrid").each(function() {
					var width = $(this).innerWidth();
					if (width) {
						$("div.gridScroller", this).width(width + "px");
					}
				});
			}
			$(window).unbind(DWZ.eventType.resizeGrid).bind("resizeGrid",
				_resizeGrid);
			//if($.fn.formatData)  $("[formatData]",$table).formatData({showColor:false});
		});
	};

	$.jTableTool = {
		getLeft: function(obj) {
			var width = 0;
			$(obj).prevAll().each(function() {
				width += $(this).outerWidth();
			});
			return width - 1;
		},
		getRight: function(obj) {
			var width = 0;
			$(obj).prevAll().andSelf().each(function() {
				width += $(this).outerWidth();
			});
			return width - 1;
		},
		getTop: function(obj) {
			var height = 0;
			$(obj).parent().prevAll().each(function() {
				height += $(this).outerHeight();
			});
			return height;
		},
		getHeight: function(obj, parent) {
			var height = 0;
			var head = $(obj).parent();
			head.nextAll().andSelf().each(function() {
				height += $(this).outerHeight();
			});
			$(".gridTbody", parent).children().each(function() {
				height += $(this).outerHeight();
			});
			return height;
		},
		getCellNum: function(obj) {
			return $(obj).prevAll().andSelf().size();
		},
		getColspan: function(obj) {
			return $(obj).attr("colspan") || 1;
		},
		getStart: function(obj) {
			var start = 1;
			$(obj).prevAll().each(function() {
				start += parseInt($(this).attr("colspan") || 1);
			});
			return start;
		},
		getPageCoord: function(element) {
			var coord = {
				x: 0,
				y: 0
			};
			while (element) {
				coord.x += element.offsetLeft;
				coord.y += element.offsetTop;
				element = element.offsetParent;
			}
			return coord;
		},
		getOffset: function(obj, evt) {
			if ($.browser.msie) {
				var objset = $(obj).offset();
				var evtset = {
					offsetX: evt.pageX || evt.screenX,
					offsetY: evt.pageY || evt.screenY
				};
				var offset = {
					offsetX: evtset.offsetX - objset.left,
					offsetY: evtset.offsetY - objset.top
				};
				return offset;
			}
			var target = evt.target;
			if (target.offsetLeft == undefined) {
				target = target.parentNode;
			}
			var pageCoord = $.jTableTool.getPageCoord(target);
			var eventCoord = {
				x: window.pageXOffset + evt.clientX,
				y: window.pageYOffset + evt.clientY
			};
			var offset = {
				offsetX: eventCoord.x - pageCoord.x,
				offsetY: eventCoord.y - pageCoord.y
			};
			return offset;
		}
	};

	//TODO 实现表格的多选功能，
	$.fn.multiTable = function(_parent) {
		var $this = $(this);
		var $parent = _parent || $this;
		var checkboxCtrls = $this.find(".checkboxCtrl");
		if (checkboxCtrls.length > 0) {
			var checkboxCtrl = $(checkboxCtrls[0]);
			var group = checkboxCtrl.attr("group");
			var selectDatas = $parent.data("selectDatas");
			if ( !! !selectDatas) {
				selectDatas = [];
				$parent.data("selectDatas", selectDatas);
			}
			$this.find(":checkbox").filter("[name='" + group + "']").click(function() {
				if ($(this).is(":checked")) {
					var obj = DWZ.jsonEval($(this).val());
					var isHas = false;
					$.each(selectDatas, function(i, value) {
						if (value.id == obj.id) {
							isHas = true;
						}
					})
					if (!isHas) {
						selectDatas.push(obj);
					}
				} else {
					var obj = DWZ.jsonEval($(this).val());
					var len = selectDatas.length-1;
					for (var i = 0; i < selectDatas.length; i++) {
						var data = selectDatas[i];
						if (obj.id == data.id) {
							if(len == i+1) {
								var tmp = selectDatas[len];
								selectDatas.splice(i, i + 1);
								selectDatas.push(tmp);
							}else {
								selectDatas.splice(i, i + 1);
							}						
						}
					}
				}
			});
		}


	}

	//实现只显示已选择的
	$.fn.showSelectDatas = function(options) {
		var opt = $.extend({
			_parent:undefined,
			isclick:false,
			curNum:false
		},options || {});
		var _parent = opt._parent;
		var isclick = opt.isclick;
		var curNum = opt.curNum;
		var $this = $(this);
		var $parent = _parent || $this;
		var selectDatas = $parent.data("selectDatas") || [];
		$pageForm = $parent.find("#pagerForm");
		var url = $pageForm.attr("action");
		var $btn = $($parent.find("button.showSelectDatas")[0]);
		if(isclick === true) {
			$btn.trigger('click');
		}
		$btn.click(function(event,curNum) {
				var ids = [];
				for (var i = 0; i < selectDatas.length; i++) {
					ids.push(selectDatas[i].id);
				}
				var data = {};
				data.selectIds = ids.join();
				if(curNum === true) {
					var pageNum = $pageForm.find("[name='pageNum']").val();
					var numPerPage = $pageForm.find("[name='numPerPage']").val();
					data.pageNum = pageNum;
					data.numPerPage = numPerPage;  
				}
				$.pdialog.reload(url, {
						data: data,
						callback: function() {
							$.pdialog.initUi($parent);
						}
				});
				$parent.data("searchType","selected");
		});
	}

	//对选择过的进行勾选
	$.fn.checkSelected = function(_parent) {
		var $this = $(this);
		var $parent = _parent || $this;
		var $p = $parent;
		var selectDatas = $parent.data("selectDatas") || [];
		var ids = [];
		for (var i = 0; i < selectDatas.length; i++) {
			ids.push(selectDatas[i].id);
		}
		var data = {};
		data.selectIds = ids.join();
		if (ids.length > 0) {
			var group = $($("[group]", $p)[0]).attr("group");
			var groups = $("input[name='" + group + "']", $p);
			for (var i = 0; i < ids.length; i++) {
				var id = ids[i];
				groups.each(function() {
					var oldObj = DWZ.jsonEval($(this).val());
					if (oldObj.id == id) {
						$(this).attr("checked", true);
					}
				})
			}

		}

	}
})(jQuery);